/*
   -- New project --
   
   This source code of graphical user interface 
   has been generated automatically by RemoteXY editor.
   To compile this code using RemoteXY library 3.1.13 or later version 
   download by link http://remotexy.com/en/library/
   To connect using RemoteXY mobile app by link http://remotexy.com/en/download/                   
     - for ANDROID 4.13.13 or later version;
     - for iOS 1.10.3 or later version;
    
   This source code is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.    
*/

//////////////////////////////////////////////
//        RemoteXY include library          //
//////////////////////////////////////////////

// you can enable debug logging to Serial at 115200
//#define REMOTEXY__DEBUGLOG    

// RemoteXY select connection mode and include library 
#define REMOTEXY_MODE__ESP32CORE_BLE

#include <BLEDevice.h>
#include <ESP32Servo.h>
// RemoteXY connection settings 
#define REMOTEXY_BLUETOOTH_NAME "RemoteXY"


#include <RemoteXY.h>

// RemoteXY GUI configuration  
#pragma pack(push, 1)  
uint8_t RemoteXY_CONF[] =   // 81 bytes
  { 255,4,0,0,0,74,0,17,0,0,0,31,1,106,200,1,1,5,0,4,
  12,66,16,65,32,2,26,4,73,66,16,65,32,2,26,4,28,169,52,17,
  160,2,26,4,43,67,16,65,32,2,26,129,12,37,78,12,17,229,186,149,
  32,32,32,32,32,32,32,233,162,136,32,32,32,32,32,32,32,232,133,176,
  0 };
  
// this structure defines all the variables and events of your control interface 
struct {

    // input variables
  int8_t slider_01; // from -100 to 100
  int8_t slider_02; // from -100 to 100
  int8_t slider_03; // from -100 to 100
  int8_t slider_04; // from -100 to 100

    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0

} RemoteXY;   
#pragma pack(pop)
 
/////////////////////////////////////////////
//           END RemoteXY include          //
/////////////////////////////////////////////

// 定义舵机对象
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

// 定义舵机引脚
const int servoPin1 = 9;
const int servoPin2 = 10;
const int servoPin3 = 18;
const int servoPin4 = 19;

void setup() 
{
  RemoteXY_Init (); 
  
  Serial.begin(115200);           //打开串口

  //设置PWM输出定时器
	// Allow allocation of all timers
	ESP32PWM::allocateTimer(0);
	ESP32PWM::allocateTimer(1);
	ESP32PWM::allocateTimer(2);
	ESP32PWM::allocateTimer(3);
  //设置Servo对象
	servo1.setPeriodHertz(50);    // standard 50 hz servo
	servo1.attach(servoPin1, 1000, 2000); // attaches the servo on pin 9 to the servo object
  servo2.setPeriodHertz(50);    // standard 50 hz servo
	servo2.attach(servoPin2, 1000, 2000); // attaches the servo on pin 10 to the servo object
  servo3.setPeriodHertz(50);    // standard 50 hz servo
	servo3.attach(servoPin3, 1000, 2000); // attaches the servo on pin 18 to the servo object
  servo4.setPeriodHertz(50);    // standard 50 hz servo
	servo4.attach(servoPin4, 1000, 2000); // attaches the servo on pin 19 to the servo object
  
  // TODO you setup code
  
}

void loop() 
{ 
  RemoteXY_Handler ();
  
  // 读取RemoteXY上的滑动条数值
  int sliderValue1 = RemoteXY.slider_01;
  int sliderValue2 = RemoteXY.slider_02;
  int sliderValue3 = RemoteXY.slider_03;
  int sliderValue4 = RemoteXY.slider_04;
  
  // 将滑动条数值映射到舵机角度范围（0-100 映射到 0-180）
  int servoAngle1 = map(sliderValue1, 0, 100, 0, 180);
  int servoAngle2 = map(sliderValue2, 0, 100, 0, 180);
  int servoAngle3 = map(sliderValue3, 0, 100, 0, 180);
  int servoAngle4 = map(sliderValue4, 0, 100, 0, 180);
  
  // 设置舵机角度
  servo1.write(servoAngle1);
  servo2.write(servoAngle2);
  servo3.write(servoAngle3);
  servo4.write(servoAngle4);
  // TODO you loop code
  // use the RemoteXY structure for data transfer
  // do not call delay(), use instead RemoteXY_delay() 


}